﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Portal.Utility;
using Portal.Web.Model;
using Portal.Web.Utility;
using Portal.Exam.Model;
using Portal.Exam.Bussiness;
using Portal.Core;

namespace Portal.Exam.Render
{
    public class UpdateExamSegment
    {
        public static AjaxOut Draw(string SiteId)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(UpdateExamSegment));
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
       

                string ExamSegmentId = (string)WebEnvironments.Request(SiteId, "ExamSegmentId");
                ExamSegmentCls OExamSegment = new ExamSegmentBll().CreateModel(SiteId, ExamSegmentId);
                if (OExamSegment == null) throw new Exception("Tham số truy cập không hợp lệ!");
                string BackUrl = "ExamSegment.aspx";
                string SelectType =
                    "<select id=drpSelectExamType>\r\n" +
                    "   <option value=\"exam\" selected>Thi thật</option>\r\n" +
                    "   <option value=\"test\">Thi thử</option>\r\n" +
                    "</select>\r\n";
                if (OExamSegment.ExamType.Equals("test"))
                {
                    SelectType =
                    "<select id=drpSelectExamType>\r\n" +
                    "   <option value=\"exam\">Thi thật</option>\r\n" +
                    "   <option value=\"test\" selected>Thi thử</option>\r\n" +
                    "</select>\r\n";
                }
                string RetHtml =
                    "<script language=javascript>\r\n" +
                    " function ClientSaveUpdateExamSegment(ExamSegmentId)\r\n" +
                    "     {\r\n" +
                    "         SiteId='" + SiteId + "';\r\n" +
                    "         ExamSegmentName=document.getElementById(\"txtExamSegmentName\").value;\r\n" +
                    "         FromDate=document.getElementById(\"txtFromDate\").value;\r\n" +
                    "         ToDate=document.getElementById(\"txtToDate\").value;\r\n" +
                    "         Year=document.getElementById(\"txtYear\").value;\r\n" +
                    "         ExamType=document.getElementById(\"drpSelectExamType\").value;\r\n" +
                    "         RetValue = Portal.Exam.Render.UpdateExamSegment.ServerSideUpdateExamSegment(SiteId, ExamSegmentId, ExamSegmentName,Year, FromDate, ToDate,  ExamType).value;\r\n" +
                    "         if(RetValue.indexOf(\"ERROR\")!=-1)\r\n" +
                    "         {\r\n" +
                    "             alert(RetValue);\r\n" +
                    "             return;\r\n" +
                    "         }\r\n" +
                    "         window.open('" + BackUrl+"','_self');\r\n"+
                    "     }\r\n" +

                    "</script>\r\n" +
                " <div class=DatabaseFunction>\r\n" +
                "        <ul>\r\n" +
                "            <li onclick=\"javascript:ClientSaveUpdateExamSegment('" + ExamSegmentId + "');\"><img class=image src=\""+IconRoot+"/Save.png\"><div>Ghi lại</div></li>\r\n" +
                "            <li onclick=\"javascript:window.open('"+BackUrl+"','_self');\"><img class=image src=\""+IconRoot+"/back.png\"><div>Quay lại</div></li>\r\n" +
                "        </ul>\r\n" +
                "  </div>\r\n" +
                "   <h3>Sửa chữa kỳ thi</h3>\r\n"+
                "   <div class=CssFormSaveUpdate>\r\n" +
                "     <table style=\"width:99%\">" +
                "          <tr>" +
                "              <td class=\"Col1\">Mô tả:</td>" +
                "              <td><input class=textbox id=txtExamSegmentName value=\"" + OExamSegment.ExamSegmentName + "\"></td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=\"Col1\">Từ ngày:</td>" +
                "              <td>" + LookUpUtilities.GenerateLookUpDate("txtFromDate", OExamSegment.FromDate) + "</td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=\"Col1\">Đến ngày</td>" +
                "              <td>" + LookUpUtilities.GenerateLookUpDate("txtToDate", OExamSegment.ToDate) + "</td>" +
                "          </tr>" +

                "          <tr>" +
                "              <td class=\"Col1\">Năm</td>" +
                "              <td><input type=textbox class=textbox id=txtYear value=\"" + OExamSegment.Year+ "\"></td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=\"Col1\">Kiểu:</td>" +
                "              <td>" + SelectType + "</td>" +
                "          </tr>" +

                //"          <tr>" +
                //"              <td></td>" +
                //"              <td><input type=button class=button onclick=javascript:ClientSaveUpdateExamSegment(); value=\"Ghi lại\"><input type=button class=button onclick=javascript:window.open('" + BackUrl + "','_self'); value=\"Thoát\"></td>" +
                //"          </tr>" +
                "      </table>" +
                "</div>\r\n";


                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage=ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideUpdateExamSegment(
            string SiteId,
            string ExamSegmentId,
            string ExamSegmentName,
            string Year,
            string FromDate,
            string ToDate,
            string ExamType)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut();

                if (string.IsNullOrEmpty(ExamSegmentName))
                {
                    throw new Exception("Chưa nhập thông tin kỳ thi!");
                }
                if (!FunctionUtilities.checkVnDate(FromDate))
                {
                    throw new Exception("Thời gian nhập không hợp lệ!");
                }
                if (!FunctionUtilities.checkVnDate(ToDate))
                {
                    throw new Exception("Thời gian nhập không hợp lệ!");
                }
                if (!FunctionUtilities.checkInteger(Year))
                {
                    throw new Exception("Năm nhập không hợp lệ!");
                }

                DateTime dFromDate = FunctionUtilities.VNDateToDate(FromDate);
                DateTime dToDate = FunctionUtilities.VNDateToDate(ToDate);
                new ExamSegmentBll().UpdateExamSegment(null, SiteId, ExamSegmentId, ExamSegmentName, int.Parse(Year), ExamType, dFromDate, dToDate);
                return "Cập nhật thành công!";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

    }
}